{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1、该数据集一共有 1247 条数据。\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "\n",
    "def count_rows_and_columns(filename):\n",
    "    with open(filename, mode='r', newline='') as file:\n",
    "        reader = csv.reader(file)\n",
    "        rows = list(reader)  # 读取所有行到列表中\n",
    "        num_rows = len(rows)  # 行数\n",
    "        num_cols = len(rows[0]) if rows else 0  # 列数（第一行包含列标题）\n",
    "        return num_rows, num_cols\n",
    "\n",
    "# 使用示例\n",
    "filename = 'data/salaries_cyber.csv'\n",
    "num_rows, num_cols = count_rows_and_columns(filename)\n",
    "print(f\"1、该数据集一共有 {num_rows-1} 条数据。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2、该数据集有 11 列特征。\n"
     ]
    }
   ],
   "source": [
    "print(f\"2、该数据集有 {num_cols} 列特征。\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3、该数据集有 4 列数值型特征，7 列类别型特征。\n"
     ]
    }
   ],
   "source": [
    "def read_row(filename, row_number):\n",
    "    with open(filename, mode='r', newline='') as file:\n",
    "        reader = csv.reader(file)\n",
    "        for i, row in enumerate(reader):\n",
    "            if i == row_number - 1:  # 注意：enumerate从0开始计数，所以需要减1\n",
    "                return row\n",
    "        # 如果文件中没有足够的行，则返回None或其他默认值\n",
    "        return None\n",
    "\n",
    "# 使用示例\n",
    "row_to_read = 2\n",
    "row_data = read_row(filename, row_to_read)\n",
    "int_num=0\n",
    "cat_num=0\n",
    "for data in row_data:\n",
    "    try:\n",
    "        data=float(data)\n",
    "        int_num+=1\n",
    "    except:\n",
    "        cat_num+=1\n",
    "print(f\"3、该数据集有 {int_num} 列数值型特征，{cat_num} 列类别型特征。\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4、该数据集有0个缺失值。\n"
     ]
    }
   ],
   "source": [
    "def count_missing_values(filename, missing_values=['', 'NA']):\n",
    "    missing_count = 0\n",
    "    with open(filename, mode='r', newline='') as file:\n",
    "        reader = csv.reader(file)\n",
    "        for row in reader:\n",
    "            for cell in row:\n",
    "                if cell.strip() in missing_values:  # 使用strip()去除前后的空格或换行符\n",
    "                    missing_count += 1\n",
    "    return missing_count\n",
    "\n",
    "missing_value_count = count_missing_values(filename)\n",
    "print(f\"4、该数据集有{missing_value_count}个缺失值。\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列 'experience_level' 类别特征中共有 4 个唯一类别\n",
      "列 'employment_type' 类别特征中共有 4 个唯一类别\n",
      "列 'job_title' 类别特征中共有 87 个唯一类别\n",
      "列 'salary_currency' 类别特征中共有 21 个唯一类别\n",
      "列 'employee_residence' 类别特征中共有 58 个唯一类别\n",
      "列 'company_location' 类别特征中共有 55 个唯一类别\n",
      "列 'company_size' 类别特征中共有 3 个唯一类别\n",
      "\n",
      "5、该数据集每个列类别型特征一共有232种唯一类别。\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('data/salaries_cyber.csv')\n",
    "categorical_columns = df.select_dtypes(include=['object', 'category']).columns\n",
    "num=0\n",
    "# 遍历每个类别型特征并计算其唯一类别数量\n",
    "for column in categorical_columns:\n",
    "    unique_values = df[column].unique()\n",
    "    num_unique_categories = len(unique_values)\n",
    "    num+=num_unique_categories\n",
    "    print(f\"列 '{column}' 类别特征中共有 {num_unique_categories} 个唯一类别\")\n",
    "print()\n",
    "print(f\"5、该数据集每个列类别型特征一共有{num}种唯一类别。\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
